package com.glela.controller;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestBaseModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;
import com.glela.statistics.model.UserEarnings;
import com.glela.statistics.model.request.StatisticsRequest;
import com.glela.statistics.model.response.SalesRanking;
import com.glela.statistics.model.response.StatisticsResponse;
import com.glela.statistics.model.response.ThisMonthAchievement;
import com.glela.statistics.service.StatisticsService;
import com.glela.statistics.service.UserEarningsService;
import com.glela.statistics.service.impl.StatisticsServiceImpl0;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 统计相关
 * 
 * @author 卫耀华
 *
 */
@RequestMapping("/statistics")
@RestController
public class StatisticsController {

	private static Logger logger = LoggerFactory.getLogger(StatisticsController.class);

	@Autowired
	private List<StatisticsService> statisticsServiceList;
	@Autowired
	private UserEarningsService userEarningsService;

	/**
	 * 店铺7天30天90天统计（校妆APP3.5期需求）
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/talentStoreStatistics", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> talentStoreStatistics(@RequestBody StatisticsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getUserId(), request.getStatisticsType(), request.getDayNumType())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			StatisticsResponse statisticsResponse = getStatisticsService().getTalentStoreStatistics(request.getUserId(),
					request.getStatisticsType(), request.getDayNumType());
			return new ResponseEntity<>(new ResponseResultModel<>(statisticsResponse), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("店铺7天30天90天统计异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * B区店铺本月业绩（校妆APP3.5期需求）
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/thisMonthAchievement", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> thisMonthAchievement(@RequestBody StatisticsRequest request) {
		try {
			if (ParameterUtils.validParams(request.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			ThisMonthAchievement statisticsResponse = getStatisticsService().getThisMonthAchievement(request.getUserId());
			logger.info("---end---thisMonthAchievement---controller---");
			return new ResponseEntity<>(new ResponseResultModel<>(statisticsResponse), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("B区店铺本月业绩统计异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 本月业绩排名榜（校妆APP3.5期需求）
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/salesRankingList", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> salesRankingList(@RequestBody StatisticsRequest request) {
		try {
			List<SalesRanking> list = getStatisticsService().getThisMonthSalesRanking(request.getSelfSupport());
			logger.info("---end---salesRankingList---controller---");
			return new ResponseEntity<>(new ResponseResultModel<>(list), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("B区店铺本月业绩统计异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 店主订单获取
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/talentOrder", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> talentOrder(@RequestBody StatisticsRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId(), model.getPageNum(), model.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().getTeacherOrderEntity(
					model.getUserId(), model.getType(), model.getStartIndex(), model.getPageSize())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取讲师订单异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 获取学生店主
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/talentStudent", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> talentStudent(@RequestBody StatisticsRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId(), model.getPageNum(), model.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(getStatisticsService().getTeacherStudentEntity(
					model.getUserId(), model.getStartIndex(), model.getPageSize())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取学生店主异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 【累计收益】：累计历史所有订单（买卖两种）收益，包含已到账收益和待收收益{待收+账户余额+总消费金额}
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getAccumulatedEarnings", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getAccumulatedEarnings(@RequestBody StatisticsRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(userEarningsService.selectEarningsWithCache(model.getUserId())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取累计收益", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 【今日收益】：付款时间在今天0点到当前时间的所有订单（包含买卖两种）佣金总和
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getTodayEarnings", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getTodayEarnings(@RequestBody StatisticsRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(userEarningsService.selectEarningsWithCache(model.getUserId())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取今日收益", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 *  【待收收益】：累计历史所有订单（买卖两种）未到帐收益总和
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getUncollectedEarnings", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getUncollectedEarnings(@RequestBody StatisticsRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(userEarningsService.selectEarningsWithCache(model.getUserId())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取待收收益", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 【本月收益】：累计付款时间在当前自然月内所有（买卖两种）订单收益，包含当前自然月内已到账收益和待收收益
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getMonthEarnings", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getMonthEarnings(@RequestBody StatisticsRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(userEarningsService.selectEarningsWithCache(model.getUserId())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取本月收益", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 【本月销售额】：累计当前自然月所有已支付订单（买卖两种）支付金额总和（实付款+余额）
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getmonthSalesVolume", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getmonthSalesVolume(@RequestBody StatisticsRequest model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(userEarningsService.selectEarningsWithCache(model.getUserId())), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取本月销售额", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 【店主通讯录数量】：暂时使用直接查询 后期做缓存
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getStoreNumber", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getStoreNumber(@RequestBody RequestBaseModel model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			UserEarnings UserEarnings=new UserEarnings();
			UserEarnings.setUserId(model.getUserId());
//			UserEarnings.setStoreNum(getStatisticsService().selectStoreNumber(model.getUserId()));
			UserEarnings.setStoreNum(getStatisticsService().selectUnderStoreNumber(model.getUserId()));
			return new ResponseEntity<>(new ResponseResultModel<>(UserEarnings), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("店主通讯录人数异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * V4.0
	 * @return
	 */
	private StatisticsService getStatisticsService(){
		for (StatisticsService userThisService : statisticsServiceList) {
			if (userThisService instanceof StatisticsServiceImpl0) {
				return userThisService;
			}
		}
		return statisticsServiceList.get(0);
	}

}
